package com.example.design01.controller;

import com.example.design01.Bean.StaffBean;
import com.example.design01.Service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class StaffController {
    @Autowired
    private StaffService staffService;

    // 页面数据展示（查全部）
    @RequestMapping("checkAll")
    public String checkAll(Model model) {
        List<StaffBean> list = staffService.checkAll();
        model.addAttribute("list", list);
        return "list";
    }

    //    添加跳转
    @RequestMapping("/toAddStaff")
    public String toAdd() {
        return "addStaff";
    }

    // 添加
    @PostMapping("/addStaff")
    public String addEmp(StaffBean staffBean) {
        staffService.addStaff(staffBean);//
        return "redirect:/checkAll";//重定向到/,刷新checkAll列表,返回到list页面
    }

    //删除
    @RequestMapping("/deleteStaff/{id}")
    public String deleteSkill(@PathVariable("id") String id) {
        staffService.deleteStaff(id);
        return "redirect:/checkAll";
    }

    //    修改跳转
    @RequestMapping("/toUpdateStaff/{id}")
    public String toUpdate(@PathVariable("id") String id, Model model) {
        StaffBean staffBean = staffService.checkById(id);
        model.addAttribute("list", staffBean);
        return "updateStaff";
    }

    //修改
    @RequestMapping("/updateStaff")
    public String updateStaff(StaffBean staffBean, Model model) {
        staffService.updateStaff(staffBean);
        StaffBean staffBean1 = staffService.checkById(staffBean.getId());
        model.addAttribute("list", staffBean1);
        return "redirect:/checkAll";
    }

    //搜索（按id查）
    @RequestMapping("/byIdStaff")
    public String checkByID(Model model, String id) {
        StaffBean staffBean = staffService.checkById(id);
        List<StaffBean> list = new ArrayList<>();
        list.add(staffBean);
        model.addAttribute("list", list);
        return "list";
    }

    //    个人信息跳转
    @RequestMapping("/toCheckByIDStaff")
    public String toCheckByID() {
        return "perInf";
    }

    //个人信息（）
    @RequestMapping("/byId")
    public String checkById(Model model, String id) {
        StaffBean staffBean = staffService.checkById(id);
        List<StaffBean> list = new ArrayList<>();
        list.add(staffBean);
        model.addAttribute("lists", list);
        return "perInf";
    }

    //    个人修改跳转
    @RequestMapping("/toUpdateStaffAdmin/{id}")
    public String toUpdateAdmin(@PathVariable("id") String id, Model model) {
        StaffBean staffBean = staffService.checkById(id);
        model.addAttribute("list", staffBean);
        return "updateStaffAdmin";
    }

    //个人修改
    @RequestMapping("/updateStaffAdmin")
    public String updateStaffAdmin(StaffBean staffBean, Model model) {
        staffService.updateStaff(staffBean);
        StaffBean staffBean1 = staffService.checkById(staffBean.getId());
        model.addAttribute("list", staffBean1);
        return "redirect:/byId";
    }
}
